<template>
  <div class="app-container">

    <el-form :inline="true" :model="serchObj" class="demo-form-inline">
      <el-form-item label="医院名称">
        <el-input v-model="serchObj.hosname" placeholder="医院名称" />
      </el-form-item>
      <el-form-item label="医院编号">
        <el-input v-model="serchObj.hoscode" placeholder="医院编号" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="getList()">查询</el-button>
      </el-form-item>

    </el-form>
    <div>
      <el-button type="danger" size="mini" @click="removeRows()">批量删除</el-button>
    </div>

    <el-table :data="list" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" label="序号" width="50" />
      <el-table-column prop="hoscode" label="编号" width="120" />
      <el-table-column prop="hosname" label="名称" width="120" />
      <el-table-column prop="contactsName" label="联系人" width="120" />
      <el-table-column prop="contactsPhone" label="联系电话" width="120" />
      <el-table-column prop="contactsPhone" label="联系电话" width="120">
        <template slot-scope="scope">
          {{ scope.row.status === 1 ? '可用' : "不可用" }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除</el-button>
          <el-button v-if="scope.row.status==1" type="danger" size="mini" icon="el-icon-delete" @click="changeSatusRow(scope.row.id, 0)">锁定</el-button>
          <el-button v-if="scope.row.status==0" type="danger" size="mini" icon="el-icon-delete" @click="changeSatusRow(scope.row.id, 1)">解锁</el-button>
          <router-link :to="'/hospSet/edit/'+scope.row.id">
            <el-button type="danger" size="mini" icon="el-icon-delete">修改</el-button>
          </router-link>

        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :current-page="current"
      :page-size="limit"
      :total="total"
      layout="total, prev, pager, next, jumper"
      style="padding: 30px 0; text-align: center;"
      @current-change="getList" />

  </div>

</template>

<script>
import { getHospSetList, removeById, batchRemove, lockHospitalSet } from '@/api/hospset'

export default {
  data() {
    return {
      current: 1,
      limit: 3,
      serchObj: {},
      list: [],
      total: 0,
      multipeSelection: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList(page = 1) {
      this.current = page
      this.listLoading = true
      getHospSetList(this.current, this.limit, this.serchObj)
        .then(
          response => {
            console.log(response)
            this.list = response.data.records
            this.total = response.data.total
            this.listLoading = false
          }
        ).catch(
          error => {
            console.log(error)
            this.listLoading = false
          }
        )
    },
    removeDataById(id) {
      this.$confirm('确定删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeById(id)
          .then(response => {
            console.log(response)
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getList()
          }).catch(error => {
            console.log(error)
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSelectionChange(selection) {
      this.multipeSelection = selection
      console.log(this.multipeSelection)
    },
    removeRows() {
      this.$confirm('批量删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var ids = []
        for (var i = 0; i < this.multipeSelection.length; i++) {
          var id = this.multipeSelection[i].id
          ids.push(id)
        }
        console.log(ids)
        batchRemove(ids)
          .then(response => {
            console.log(response)
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getList()
          }).catch(error => {
            console.log(error)
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    changeSatusRow(id, status) {
      this.$confirm('更改状态?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        lockHospitalSet(id, status)
          .then(response => {
            console.log(response)
            this.$message({
              type: 'success',
              message: '更改成功!'
            })
            this.getList()
          }).catch(error => {
            console.log(error)
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消更改'
        })
      })
    },
    goToUpdate(id, status) {
      this.$router.push({ path: '/hospSet/edit' })
    }
  }
}
</script>
